﻿Public Class ContentProperty
    Inherits MysteryPropertyAttribute


    Public Property label As String

    Public Overrides Sub setup()
        If String.IsNullOrEmpty(label) Then label = name
    End Sub
End Class

Public MustInherit Class ContentPropertyValue
    Public MustOverride Function getValue() As Object
    Public MustOverride Sub setValue(value As Object)
    Public MustOverride Function isFilter() As Boolean
End Class

Public Class ContentPropertyValue(Of T)
    Inherits ContentPropertyValue

    Public Property value As T

    Public Sub New()

    End Sub

    Public Sub New(value As T)
        Me.value = value
    End Sub

    Public Shared Operator &(one As ContentPropertyValue(Of T), two As String) As String
        If one Is Nothing Then Return two
        Try
            Return one.value.ToString & two
        Catch ex As Exception
            Return two
        End Try
    End Operator

    Public Shared Widening Operator CType(ByVal initialData As T) As ContentPropertyValue(Of T)
        Return New ContentPropertyValue(Of T) With {.value = initialData}
    End Operator

    Public Shared Narrowing Operator CType(ByVal initialData As ContentPropertyValue(Of T)) As T
        If initialData Is Nothing Then Return Nothing
        Return initialData.value
    End Operator

    Public Overrides Function getValue() As Object
        Return value
    End Function

    Public Overrides Function isFilter() As Boolean
        Return False
    End Function

    Public Overrides Function ToString() As String
        If value Is Nothing Then Return Nothing
        Return value.ToString()

    End Function

    Public Overrides Sub setValue(value As Object)
        Me.value = value
    End Sub
End Class

Public Interface IContentPropertyFilter
    Function doesMatch(value As ContentPropertyValue) As Boolean
    Function getValues() As IEnumerable(Of Object)
    Sub setValues(values As IEnumerable(Of Object))
End Interface

Public Class ContentPropertyFilter(Of T)
    Inherits ContentPropertyValue(Of T)
    Implements IContentPropertyFilter

    Public Property values As ICollection(Of T) = New HashSet(Of T)

    Public Sub New()

    End Sub

    Public Sub New(value As T)
        Me.value = value
    End Sub

    Public Sub New(values As IEnumerable(Of T))
        Me.values = New HashSet(Of T)(values)
    End Sub


    Public Overrides Function isFilter() As Boolean
        Return True
    End Function

    Public Function doesMatch(value_to_match As ContentPropertyValue) As Boolean Implements IContentPropertyFilter.doesMatch
        Dim typed_value As T = If(value_to_match Is Nothing, Nothing, value_to_match.getValue())
        If values Is Nothing OrElse values.Count = 0 Then
            If value Is Nothing AndAlso typed_value Is Nothing Then Return True
            If value Is Nothing AndAlso typed_value IsNot Nothing Then Return False
            If typed_value Is Nothing Then Return False
            Return Me.value.Equals(typed_value)
        End If
        Return Me.values.Contains(typed_value)
    End Function

    Public Function getValues() As System.Collections.Generic.IEnumerable(Of Object) Implements IContentPropertyFilter.getValues
        If values Is Nothing OrElse values.Count = 0 Then
            Return New HashSet(Of Object) From {value}
        End If
        Return New HashSet(Of Object)(From x As Object In values Select x)
    End Function

    Public Sub setValues(values As System.Collections.Generic.IEnumerable(Of Object)) Implements IContentPropertyFilter.setValues
        For Each v As Object In values
            Me.values.Add(v)
        Next
    End Sub
End Class